package Offer;

public class Test29 
{

	public static void main(String[] args) 
	{
          int[] array ={1,2,3,2,2,2,5,4}; 
          System.out.println(moreThanHalfOfNum( array));
	}
    public static int moreThanHalfOfNum(int[] array)
    {
    	if(array==null||array.length==0)
    		return 0;
    	int result=array[0];
    	int count = 1;
    	for(int i=1;i<array.length;i++)
    	{
    		if(array[i]==result)
    		{
    			count++;
    		}
    		else
    		{
    			count--;
    		}
    		if(count==0)
    		{
    			result=array[i];
    			count=1;
    		}
    	}
    	int number=0;
    	for(int j=0;j<array.length;++j)
    	{
    		if(array[j]==result)
    		{
    			number++;
    		}
    	}
    	if(number>array.length/2)
    		return result;
    	return 0;
    }
}
